package com.LFLWZ.controller;

import com.LFLWZ.beans.Dto.Dto;
import com.LFLWZ.beans.POJO.Column;
import com.LFLWZ.beans.Vo.ColumnVo;
import com.LFLWZ.service.ColumnService;
import com.LFLWZ.utils.DtoUtil;
import com.LFLWZ.utils.EmptyUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("column/")
public class ColumnController {
    @Reference
    private ColumnService columnService;

    /***
     * 新增专栏
     * @param columnVo
     * @return
     */
    @PostMapping("ins")
    public Dto ins(@RequestBody ColumnVo columnVo){
        Dto dto = null;
        int line = 0;
        Integer id = columnVo.getId();
        Column column = new Column();
        column.setId(columnVo.getId());
        column.setImgUrl(columnVo.getImgUrl());
        column.setTitle(columnVo.getTitle());
        column.setSubtitle(columnVo.getSubtitle());
        column.setSummary(columnVo.getSummary());
        column.setStatus(columnVo.getStatus());
        column.setPrice(columnVo.getPrice());
        column.setIsfree(columnVo.getIsfree());
        List<Column> list = columnService.sel(column);
        if(EmptyUtils.isEmpty(list)) {
            line = columnService.ins(columnVo);
        }else{
            line = columnService.colup(column);
        }
        if(line > 0){
            return DtoUtil.returnError(200,"成功");
        }else{
            return DtoUtil.returnError(4001,"失败");
        }

    }

    /***
     * 根据用户ID查询发布专栏
     * @param uid 用户ID
     * @return 专栏数据
     */
    @GetMapping("selall/{uid}")
    public Dto selall(@PathVariable Integer uid){
        Dto dto = null;
        if(uid==0){
            uid =null;
        }
        List list = columnService.selall(uid);
        if(EmptyUtils.isEmpty(list)){
            return DtoUtil.returnError(4001,"数据为空,查询失败");
        }else{
            return DtoUtil.returnSuccessData(list);
        }
    }

    /***
     * 根据主键查询 专栏详情
     * @param id    主键
     * @return  详情数据
     */
    @GetMapping("selById/{id}")
    public Dto selById(@PathVariable Integer id){
        Dto dto = null;
        List<ColumnVo> list = columnService.selById(id);
        if(EmptyUtils.isEmpty(list)){
            return DtoUtil.returnError(4001,"数据为空,查询失败");
        }else{
            return DtoUtil.returnSuccessData(list);
        }
    }

    /***
     * 编辑专栏详情
     * @param column 实体类
     * @return 影响行数,是否成功
     */
    @PostMapping("colup")
    public Dto colup(@RequestBody Column column){
        int line = columnService.colup(column);
        if(line >0){
            return DtoUtil.returnError(200,"修改成功");
        }else{
            return DtoUtil.returnError(4001,"修改失败");
        }
    }
    @GetMapping("selsta")
    public Dto selsta(Column column){
        List<Column> list = columnService.selsta(column);
        if(EmptyUtils.isEmpty(list)){
            return DtoUtil.returnError(4001,"查询失败");
        }else {
           return DtoUtil.returnSuccessData(list);
        }
    }
    @GetMapping("del/{id}")
    public Dto del(@PathVariable Integer id){
        int line = columnService.del(id);
        if(line > 0){
            return DtoUtil.returnSuccessData(line);
        }else{
            return DtoUtil.returnError(4001,"添加失败");
        }
    }
}
